$(function () {
    // 左侧更新数据
    function leftData(Arr) {
        var str = '';
        for (let i in Arr) {
            str += "<ul><li><input type='checkbox' id='" + Arr[i].id + "'/></li><li>" + Arr[i].SN + "</li><li>" + Arr[i].Room + "</li></ul>";
        }
        $(".leftUpdate").html(str);
    }
    //右侧更新数据
    function rightData(Arr) {
        var str = '';
        for (let i in Arr) {
            str += "<ul><li><input type='checkbox' id='" + Arr[i].id + "'/></li><li>" + Arr[i].SN + "</li><li>" + Arr[i].time + "</li><li>" + Arr[i].Room + "</li></ul>";
        }
        $(".rightUpdate").html(str);    
    }
    // 发送数据
    function send(url) {
        let arr;
        $.ajax({
            type: "GET",
            url,
            dataType: "json",
            async: false,
            success: function (data) {
                arr = data;
            }
        });
        return arr;
    }
    // 左
    let leftArr = [];
    // 右
    let rightArr = [];
    // 临时
    let temArr = [];
    let keyL = '';
    let keyR = '';
    // 选中时段的值
    let selectValue = $("#selectValue").children("option:selected").val();
    let data = send("../json/control.json");
    leftArr = data;
    leftData(leftArr);
    $(document).on("change", ".leftUpdate input:checkbox", function () {
        keyL = $(this).prop("id");
        if ($(this).prop("checked") == true) {
            temArr[keyL] = leftArr[keyL];
        } else {
            leftArr[keyL] = temArr[keyL];
            delete temArr[keyL];
        }
        // console.log(temArr,1);
        // console.log(leftArr,2);
    })
    $(document).on("change", ".rightUpdate input:checkbox", function () {
        keyR = $(this).prop("id")
        if ($(this).prop("checked") == true) {
            temArr[keyR] = rightArr[keyR];
        } else {
            rightArr[keyR] = temArr[keyR];
            delete temArr[keyR];
        }

    })

    $(".selectValue").on("change", function () {
        selectValue = $(this).children("option:selected").val();
        console.log(selectValue,"select");
        console.log(temArr,"temArr");
        if (!temArr.length) {
            return;
        }
    })
    $("#btn1").on("click", function () {
        for (let key in temArr) {
            delete leftArr[key];
            temArr[keyL].time = selectValue;
        }
        if (selectValue) {
            rightArr= temArr;
            temArr = []; //清空
            leftData(leftArr);
            rightData(rightArr)
        }
        console.log(leftArr,"lft");
        console.log(rightArr,"right");
        console.log(temArr, "tem"); 
    });
    $("#btn2").on("click", function () {
        // console.log(temArr, "tem");
        for (let key in temArr) {
            delete rightArr[key];
        }
        if (selectValue) {
            leftArr[keyR]= temArr[keyR];
            temArr = []; //清空
            leftData(leftArr);
            rightData(rightArr);
            // console.log(leftArr,"lft");
            // console.log(rightArr,"right");
            // console.log(temArr, "tem"); 
        }
    });

})